/***************************************************************************
 * The Open Armory API is a project to retrieve World of Warcraft(R) 
 * related XML data from The Armory.
 * 
 * The data retrieved via the Open Armory API from The Armory is 
 * proprietary to Blizzard Entertainment, Inc(R).
 * *************************************************************************
 * Copyright (C) 2009 - 2010 Tedla M.B. Brandsema
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ***************************************************************************/
package net.openarmory.core.domain.url;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import net.openarmory.core.config.ConfigurationProperties;

import org.apache.log4j.Logger;

// TODO: Auto-generated Javadoc
/**
 * <p>
 * The Class <b>ArmoryURLBuilder</b> is responsible for creating different types
 * of Armory URL's.
 * </p>
 */
class ArmoryUrlBuilder {

	/** The log. */
	private static final Logger LOG = Logger.getLogger(ArmoryUrlBuilder.class);

	/** The CHARACTER NAME URL VARIABLE. */
	public static final String CHARACTER_NAME_URL_VARIABLE;

	/** The REALM URL VARIABLE. */
	public static final String REALM_URL_VARIABLE;

	/** The GUILD URL VARIABLE. */
	public static final String GUILD_URL_VARIABLE;

	/** The ITEM URL VARIABLE. */
	public static final String ITEM_URL_VARIABLE;

	/** The SLOT URL VARIABLE. */
	public static final String SLOT_URL_VARIABLE;

	/** The QUERY STRING SEPERATOR. */
	public static final String QUERY_STRING_SEPERATOR;

	/** The QUERY VARIABLE SEPERATOR. */
	public static final String QUERY_VARIABLE_SEPERATOR;

	static {
		ConfigurationProperties config = null;
		try {
			config = ConfigurationProperties.getInstance();
		} catch (final IOException e) {
			LOG.fatal("Can not open/find configuration file.", e);
		}

		CHARACTER_NAME_URL_VARIABLE = config.getCharacterNameUrlVariable();
		REALM_URL_VARIABLE = config.getRealmUrlVariable();
		GUILD_URL_VARIABLE = config.getGuildUrlVariable();
		ITEM_URL_VARIABLE = config.getItemUrlVariable();
		SLOT_URL_VARIABLE = config.getSlotUrlVariable();
		QUERY_STRING_SEPERATOR = config.getQueryStringSeperator();
		QUERY_VARIABLE_SEPERATOR = config.getQueryVariableSeperator();
	}

	/**
	 * Url encode.
	 * 
	 * @param unencoded
	 *            the unencoded
	 * @return the string
	 */
	public String urlEncode(final String unencoded) {
		try {
			return URLEncoder.encode(unencoded, "UTF-8");
		} catch (final UnsupportedEncodingException e) {
			LOG.fatal("Unsupported url encoding for: " + unencoded, e);
		}
		return null;
	}

}
